package com.ems.sys.service.impl;
import com.ems.sys.dao.DepartmentMapper;
import com.ems.sys.dao.EmployeeMapper;
import com.ems.sys.dao.PositionMapper;
import com.ems.sys.pojo.Department;
import com.ems.sys.pojo.Employee;
import com.ems.sys.service.EmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;
/****
 * @Author:传智播客
 * @Description:Emlpoyee业务层接口实现类
 * @Date 2019/6/14 0:16
 *****/
@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper emlpoyeeMapper;

    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private PositionMapper positionMapper;

    /**
     * Emlpoyee条件+分页查询
     * @param emlpoyee 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public PageInfo<Employee> findPage(Employee emlpoyee, int page, int size){
        //分页
        PageHelper.startPage(page,size);
        //搜索条件构建
        Example example = createExample(emlpoyee);
        List<Employee> employees = emlpoyeeMapper.selectByExample(example);
        for (int i=0;i<employees.size();i++){
            employees.get(i).setDepartment(departmentMapper.selectByPrimaryKey(employees.get(i).getdNum()));
            employees.get(i).setPosition(positionMapper.selectByPrimaryKey(employees.get(i).getpNum()));
        }
        //执行搜索
        return new PageInfo<Employee>(employees);
    }

    @Override
    public List<Employee> findByEId() {
        return emlpoyeeMapper.findByEId();
    }

    /**
     * Emlpoyee分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public PageInfo<Employee> findPage(int page, int size){
        //静态分页
        PageHelper.startPage(page,size);
        //分页查询
        return new PageInfo<Employee>(emlpoyeeMapper.findByEId());
    }

    /**
     * Emlpoyee条件查询
     * @param emlpoyee
     * @return
     */
    @Override
    public List<Employee> findList(Employee emlpoyee){
        //构建查询条件
        Example example = createExample(emlpoyee);
        //根据构建的条件查询数据
        return emlpoyeeMapper.selectByExample(example);
    }


    /**
     * Emlpoyee构建查询对象
     * @param emlpoyee
     * @return
     */
    public Example createExample(Employee emlpoyee){
        Example example=new Example(Employee.class);
        Example.Criteria criteria = example.createCriteria();
        if(emlpoyee!=null){
            // 工员id
            if(!StringUtils.isEmpty(emlpoyee.geteId())){
                    criteria.andEqualTo("eId",emlpoyee.geteId());
            }
            // 部门编号
            if(!StringUtils.isEmpty(emlpoyee.getdNum())){
                    criteria.andEqualTo("dNum",emlpoyee.getdNum());
            }
            // 职位编号
            if(!StringUtils.isEmpty(emlpoyee.getpNum())){
                    criteria.andEqualTo("pNum",emlpoyee.getpNum());
            }
            // 员工名称
            if(!StringUtils.isEmpty(emlpoyee.geteName())){
                    criteria.andEqualTo("eName",emlpoyee.geteName());
            }
            // 身份证
            if(!StringUtils.isEmpty(emlpoyee.getCard())){
                    criteria.andEqualTo("card",emlpoyee.getCard());
            }
            // 生日
            if(!StringUtils.isEmpty(emlpoyee.getBirthday())){
                    criteria.andEqualTo("birthday",emlpoyee.getBirthday());
            }
            // 性别
            if(!StringUtils.isEmpty(emlpoyee.getSex())){
                    criteria.andEqualTo("sex",emlpoyee.getSex());
            }
            // 电话号码
            if(!StringUtils.isEmpty(emlpoyee.getPhone())){
                    criteria.andEqualTo("phone",emlpoyee.getPhone());
            }

        }
        return example;
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(String id){
        emlpoyeeMapper.deleteByPrimaryKey(id);
    }

    /**
     * 修改Emlpoyee
     * @param emlpoyee
     */
    @Override
    public void update(Employee emlpoyee){
        emlpoyeeMapper.updateByPrimaryKey(emlpoyee);
    }

    /**
     * 增加Emlpoyee
     * @param emlpoyee
     */
    @Override
    public void add(Employee emlpoyee){
        emlpoyeeMapper.insert(emlpoyee);
    }

    /**
     * 根据ID查询Emlpoyee
     * @param id
     * @return
     */
    @Override
    public Employee findById(String id){
        return  emlpoyeeMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询Emlpoyee全部数据
     * @return
     */
    @Override
    public List<Employee> findAll() {
        return emlpoyeeMapper.selectAll();
    }
}
